<?php
class Form {

	var $action = "";
	var $arr;
	var $default_width = 300;
	var $default_height = 100;
	var $data = array();
	var $row = array();

	function Form($arr){
		$this->arr = $arr;
	}

	function add_data($data) {
		$this->data = $data;
    }

	function compile_form(){
		foreach($this->arr as $k => $v){
			$tmp = array();
			$tmp['key'] = $k;
			$tmp['type'] = $v['type'];
			$tmp['style'] = $v['style'];
			$tmp['desc'] = $v['desc'];
			$tmp['require'] = $v['require'];

			$tmp['title'] = ($v['title'])?$v['title']:ucfirst($k);
			$tmp['width'] = ($v['width'])?$v['width']:$this->default_width;
			$tmp['height'] = ($v['height'])?$v['height']:$this->default_height;

			if($v['edit'] === false){
				$v['edit'] = "readonly='readonly'";
			}

			switch ($v['type']) {
				case "text" : 
					$tmp['form'] = "<input type = 'text' id = '$k' class = 'input w".$tmp['width']."' name = '$k' ".$v['edit'].">\n";
					break;

				case "upload" :
					$tmp['form'] = "<input type = 'file' name = '$k'>\n";
					break;

				case "textarea" :
					if($v['RTE']){
						$rte = "mce";
					}

					$tmp['form'] = "<textarea class = '".$rte." w".$tmp['width']." h".$tmp['height']."' name = '".$k."' ".$v['edit']."></textarea>\n";
					break;

				case "hidden" :
					$tmp['form'] = "<input type = 'hidden' name = '$k'>";
					break;

				case "date" : 
					$tmp['form'] = "<input type = 'text' class = 'input w".$tmp['width']."' id = '".$k."' name = '$k' ".$v['edit'].">\n";
					$tmp['js'] .= "<script>$(function() {";
					$tmp['js'] .= "$( '#".$k."' ).datepicker({dateFormat: 'yy-mm-dd'});";
					$tmp['js'] .= "}); </script>";

					break;

				case "select" :
					$tmp['form'] = "<select name = '$k'>";

					foreach($v['data'] as $i => $str){
						if($this->data[$k] == $i){
							$tmp['form'] .= "<option value = '".$i."' selected>".$str."</option>";
						}else{
							$tmp['form'] .= "<option value = '".$i."'>".$str."</option>";
						}
					}

					$tmp['form'] .= "</select>";
					break;

				case "radio" :

					foreach($v['data'] as $i => $str){
						if($this->data[$k] == $i){
							$tmp['form'] .= "<input type = 'radio' name = '$k' value = '".$i."' id = '$k$i' checked><label class = 'radio' for='$k$i'>".$str."</label>\n";
						}else{
							$tmp['form'] .= "<input type = 'radio' name = '$k' value = '".$i."' id = '$k$i'><label class = 'radio' for = '$k$i'>".$str."</label>\n";
						}
					}

					break;

				case "checkbox" :

					foreach($v['data'] as $i => $str){
						if($this->data[$k] == $i){
							$tmp['form'] .= "<input type = 'checkbox' name = '".$k."[]' value = '".$i."' id = '$k$i' checked><label class = 'radio' for='$k$i'>".$str."</label>\n";
						}else{
							$tmp['form'] .= "<input type = 'checkbox' name = '".$k."[]' value = '".$i."' id = '$k$i'><label class = 'radio' for = '$k$i'>".$str."</label>\n";
						}
					}

					break;


				default : $tmp['form'] = "<input type = 'text' class = 'input w".$tmp['width']."' id = '$k' name = '$k' ".$v['edit'].">\n";
					break;

			}

			if($tmp['require']){
				$tmp['form'] = str_replace("class = 'input","class = 'input validate[required] text-input",$tmp['form']);
				$tmp['form'] = $tmp['form']. "<span class='require'>*</span>";
			}

			if(isset($v['lang'])){
				$langs = explode("/", $v['lang']);
				$tmp_form = $tmp['form'];
				foreach($langs as $lang){
					$tmp['form'] = preg_replace('/name = \'(\w+)\'/i', 'name = \'${1}_'.$lang.'\'', $tmp_form);
					$tmp['lang'] = "<img src='".URL."admin/html/images/".$lang.".gif'>";
					array_push($this->row, $tmp);
				}
			}else{
				array_push($this->row, $tmp);
			}
		}
	}

	function compile_data(){
		foreach($this->row as $key => $value){

			if($this->arr[$value['key']]['value']){
				// make hidden value for real value
				$this->row[$key]['hidden'] = "<input type = 'hidden' name = '_".$this->data[$value['key']]."'>\n";

				// replace with setting value of use call_back_function
				if(strstr($this->arr[$value['key']]['value'], "function")){

					$function_name = trim(str_replace("function", "", $this->arr[$value['key']]['value']));
					$function_name = str_replace("()", "", $function_name);

					if ( !function_exists('$function_name') ) :
						$this->data[$value['key']] = call_user_func($function_name);
					endif;

				}else{
					$this->data[$value['key']] = $this->arr[$value['key']]['value'];
				}
			}

			switch ($value['type']) {
				case "text" : 
					$this->row[$key]['form'] = str_replace(">", " value = '".$this->data[$value['key']]."'>", $value['form']);
					break;

				case "upload" :
					$this->row[$key]['form'] = str_replace("<", "<img src = '".$this->data[$value['key']]."'><br/><", $value['form']);
					break;

				case "textarea" :
					$this->row[$key]['form'] = str_replace("><", ">".$this->data[$value['key']]."<", $value['form']);
					break;

				case "hidden" :
					$this->row[$key]['form'] = str_replace(">", " value = '".$this->data[$value['key']]."'>", $value['form']);
					break;

				case "datetime" :
					$this->row[$key]['form'] = str_replace(">", " value = '".$this->data[$value['key']]."'>", $value['form']);
					break;

				case "select" : break;

				default :
					$this->row[$key]['form'] = str_replace(">", " value = '".$this->data[$value['key']]."'>", $value['form']);
					break;
			}

//			$this->row[$key]['form'] .= "<input type = 'hidden' name = '$k' value = '".$this->data[$value['key']]."'>";
		}
	}

	function add_action($act){
		global $dsfm_module;
		$this->action = URL . "admin/".$dsfm_module."/".$act;
	}

	function compile(){
		$this->compile_form();

		if(count($this->data)){
			$this->compile_data();
		}
	}

	function rain_display($tpl, $template = "form"){

		$this->compile();

//		$tpl->assign("table_menu", $this->submodule);
//		$tpl->assign("title", $this->form);
		$tpl->assign("row", $this->row);
		$tpl->assign("action", $this->action);
		$tpl->draw($template);
	}
}

?>